2020-2021 Sunseeker Telemetry and Lighting System
usci_a_spi_ex1_master.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
75 //*****************************************************************************
76 
77 //*****************************************************************************
78 //
79 //Specify desired frequency of SPI communication
80 //
81 //*****************************************************************************
82 #define SPICLK 500000
83 
84 uint8_t transmitData = 0x00, receiveData = 0x00;
85 uint8_t returnValue = 0x00;
86 
87 void main (void)
88 {
89  //Stop watchdog timer
90  WDT_A_hold(WDT_A_BASE);
91 
92  //Set P1.1 for slave reset
94 
95  GPIO_PORT_P1,
96  GPIO_PIN1
97  );
98 
99  //Set P1.1 for slave reset
100  //Set P1.0 to output direction
101  GPIO_setAsOutputPin(
102  GPIO_PORT_P1,
103  GPIO_PIN0
104  );
105 
106  //P3.5,4,0 option select
107  GPIO_setAsPeripheralModuleFunctionInputPin(
108  GPIO_PORT_P3,
109  GPIO_PIN5 + GPIO_PIN4 + GPIO_PIN0
110  );
111 
112  //Initialize Master
113  USCI_A_SPI_initMasterParam param = {0};
114  param.selectClockSource = USCI_A_SPI_CLOCKSOURCE_SMCLK;
115  param.clockSourceFrequency = UCS_getSMCLK();
116  param.desiredSpiClock = SPICLK;
117  param.msbFirst = USCI_A_SPI_MSB_FIRST;
118  param.clockPhase = USCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
119  param.clockPolarity = USCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
120  returnValue = USCI_A_SPI_initMaster(USCI_A0_BASE, &param);
121 
122  if (STATUS_FAIL == returnValue){
123  return;
124  }
125 
126  //Enable SPI module
127  USCI_A_SPI_enable(USCI_A0_BASE);
128 
129  //Enable Receive interrupt
130  USCI_A_SPI_clearInterrupt(USCI_A0_BASE,
131  USCI_A_SPI_RECEIVE_INTERRUPT);
132  USCI_A_SPI_enableInterrupt(USCI_A0_BASE,
133  USCI_A_SPI_RECEIVE_INTERRUPT);
134 
135  //Now with SPI signals initialized, reset slave
137  GPIO_PORT_P1,
138  GPIO_PIN1
139  );
140 
141  //LED On
143  GPIO_PORT_P1,
144  GPIO_PIN0
145  );
146 
147  //Wait for slave to initialize
148  __delay_cycles(100);
149 
150  //Initialize data values
151  transmitData = 0x00;
152 
153  //USCI_A0 TX buffer ready?
154  while (!USCI_A_SPI_getInterruptStatus(USCI_A0_BASE,
155  USCI_A_SPI_TRANSMIT_INTERRUPT)) ;
156 
157  //Transmit Data to slave
158  USCI_A_SPI_transmitData(USCI_A0_BASE, transmitData);
159 
160  //CPU off, enable interrupts
161  __bis_SR_register(LPM0_bits + GIE);
162 }
163 
164 //******************************************************************************
165 //
166 //This is the USCI_B0 interrupt vector service routine.
167 //
168 //******************************************************************************
169 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
170 #pragma vector=USCI_A0_VECTOR
171 __interrupt
172 #elif defined(__GNUC__)
173 __attribute__((interrupt(USCI_A0_VECTOR)))
174 #endif
175 void USCI_A0_ISR (void)
176 {
177  switch (__even_in_range(UCA0IV,4)){
178  //Vector 2 - RXIFG
179  case 2:
180  //USCI_A0 TX buffer ready?
181  while (!USCI_A_SPI_getInterruptStatus(USCI_A0_BASE,
182  USCI_A_SPI_TRANSMIT_INTERRUPT)) ;
183 
184  receiveData = USCI_A_SPI_receiveData(USCI_A0_BASE);
185 
186  //Increment data
187  transmitData++;
188 
189  //Send next value
190  USCI_A_SPI_transmitData(USCI_A0_BASE,
192  );
193 
194  //Delay between transmissions for slave to process information
195  __delay_cycles(40);
196 
197  break;
198  default: break;
199  }
200 }
201 
MPU_initThreeSegmentsParam param
#define STATUS_FAIL
Definition: hw_memmap.h:23
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)
uint8_t receiveData
void main(void)
void USCI_A0_ISR(void)
uint8_t transmitData
uint8_t returnValue
#define SPICLK